<?=view('header.html')?>

<body class="layui-view-bodys">
    <div class="layui-form-iframe">
        <div class="layui-form mokui-form layui-card-body">
            <div class="layui-form-item">
                <label class="layui-form-label">选择上级:</label>
                <div class="layui-input-inline ">
                    <select class="" name="data[pid]">
                        <option value="">作为上级分类</option>
                        <?php if($cats):?>
                        <?php foreach($cats as $p):?>
                        <option value="<?=$p['id']?>"><?=$p['spacer']?><?=$p['name']?></option>
                        <?php endforeach?>
                        <?php endif?>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分类名称:</label>
                <div class="layui-input-block">
                    <input type="text" name="data[name]" value="<?=isset($data['name']) ? $data['name'] : ''?>" lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">英文名称:</label>
                <div class="layui-input-block">
                    <input type="text" name="data[enname]" value="<?=isset($data['enname']) ? $data['enname'] : ''?>" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">ICON:</label>
                <div class="layui-input-block">
                    <input type="text" name="data[icon]" value="<?=isset($data['icon']) ? $data['icon'] : ''?>" placeholder="图标或其他" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否显示:</label>
                <div class="layui-input-block">
                    <input type="radio" name="data[show]" <?=((isset($data[ 'show']) && $data[ 'show']==1) || !isset($data[ 'show'])) ? 'checked' : ''?> value="1" title="显示">
                    <input type="radio" name="data[show]" <?=(isset($data[ 'show']) && $data[ 'show']=='0' ) ? 'checked' : ''?> value="0" title="不显示">
                </div>
            </div>
            <div class="layui-form-item vars_file">
                <div class="layui-inline">
                    <label class="layui-form-label">图片:</label>
                    <div class="layui-input-block">
                        <div class="layui-upload file">
                            <div class="mokui-upload-list-wrapper">
                                <ul class="mokui-sortable mokui-image-upload-list">

                                    <?php if(isset($data['thumb_info']) && $data['thumb']):?>
                                    <li class="mokui-upload-item" data-fileid="<?=$data['thumb']?>">
                                        <img class="mokui-upload-item-thumb" alt="<?=$data['thumb_info']['filename']?>" data-src="/writable/<?=$data['thumb_info']['attachment']?>" src="<?=ams_thumb($data['thumb_info']['attachment'], 200, 200)?>">
                                        <input type="hidden" value="<?=$data['thumb']?>" name="data[thumb]" />
                                        <span class="mokui-upload-item-delete-span layui-icon layui-icon-close-fill mokui-upload-item-delete"></span>
                                    </li>
                                    <?php endif?>

                                    <div class="mokui-upload-btn-div" id="file">
                                        <i class="layui-icon layui-icon-addition mokui-upload-add"></i>
                                    </div>
                                </ul>
                                <div class="mokui-upload-tips">建议尺寸 640*640，最多 1 张，单张图片不超过 2M</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">简介描述：</label>
                <div class="layui-input-block">
                    <textarea name="data[description]" autocomplete="off" class="layui-textarea" id=""><?=isset($data['description']) ? $data['description'] : ''?></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-blue" lay-submit="" lay-filter="ams_submit">提交保存</button>
                </div>
            </div>
        </div>
    </div>
    <?=view('footer.html')?>
    <script type="text/javascript">
        layui.use(['layer', 'jquery', 'form', 'upload'], function() {
            var layer = layui.layer,
                $ = layui.jquery,
                form = layui.form,
                upload = layui.upload;

            form.on('submit(ams_submit)', function(data) {
                console.log(data);
                $.post('', data.field, function(res) {
                    console.log(res);
                    if (res.code == 0) {
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 1000
                        }, function() {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            // parent.location.href = adminpath + '/role/index';
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 1500,
                            anim: 6
                        });
                    }
                }, 'json');
                return false;
            });

            var uploadFile = upload.render({
                elem: '#file',
                url: adminpath + 'news/admin/home/upload',
                accept: 'file',
                method: 'post',
                data: {},
                auto: true,
                field: 'file',
                multiple: false,
                number: 1,
                choose: function(obj) {},
                before: function(obj) {
                    layer.load();
                },
                done: function(res, index, upload) {
                    var len = $(".file .mokui-image-upload-list .mokui-upload-item").length;
                    if (len >= 1) {
                        //删除
                        var fid = res.data.fileid;
                        console.log(fid);
                        ams_del_attach(fid);
                        layer.msg('只能上传一个文件', {
                            time: 1500,
                            anim: 6
                        });
                    } else if (res.code == '0' && len <= 0) {
                        var str = '';
                        str += '<li class="mokui-upload-item" data-fileid="' + res.data.fileid + '"><img class="mokui-upload-item-thumb" alt="' + res.data.title + '" data-src="' + res.data.src + '" src="' + res.data.thumb + '">';
                        str += '<input type="hidden" value="' + res.data.fileid + '" name="data[thumb]" />';
                        str += '<span class="mokui-upload-item-delete-span layui-icon layui-icon-close-fill mokui-upload-item-delete"></span></li>';
                        $(".file .mokui-image-upload-list .mokui-upload-btn-div").before(str);
                    } else {
                        layer.msg(res.msg, {
                            time: 1500,
                            anim: 6
                        });
                    }
                    layer.closeAll('loading');
                },
                error: function(index, upload) {
                    layer.closeAll('loading');
                }
            });

        });
    </script>
</body>

</html>